package com.daxt.utils;

import com.daxt.base.common.util.RedisUtil;
import com.daxt.model.dic.SystemConfigType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 *
 * @author: lig
 * @date: 2021-07-23
 */
public class DataBaseUtil {

    private static final Log log = LogFactory.getLog(DataBaseUtil.class);

//    private static String mysqlBackupDir;
//    private static String mysqlDir;
//    private static String ip;
//
//    static{
//        mysqlBackupDir = RedisUtil.getSysConfig(SystemConfigType.MYSQL_BACKUP_DIR);
//        mysqlDir = RedisUtil.getSysConfig(SystemConfigType.MYSQL_DIR);
//        ip = RedisUtil.getSysConfig(SystemConfigType.MYSQL_IP);
//
//        if (!mysqlBackupDir.endsWith(File.separator)) mysqlBackupDir += File.separator;
//        if (!mysqlDir.endsWith(File.separator)) mysqlBackupDir += File.separator;
//    }



    /**
     * @description: 操作mysql命令
     *
     * @param type auto自动  manual手动
     * @param flag restore恢复还原  backup备份
     *
     *
     * @author: lig
     * @date: 2021/7/23
     */
    public static boolean execMysql(String fileName,String mysqlusername,String mysqlpassword,String type,String flag,String port,String dataBaseName){
        try {

            String mysqlBackupDir = RedisUtil.getSysConfig(SystemConfigType.MYSQL_BACKUP_DIR);
            String mysqlDir = RedisUtil.getSysConfig(SystemConfigType.MYSQL_DIR);
            String ip = RedisUtil.getSysConfig(SystemConfigType.MYSQL_IP);

//            if (!mysqlBackupDir.endsWith(File.separator)) mysqlBackupDir += File.separator;
//            if (!mysqlDir.endsWith(File.separator)) mysqlBackupDir += File.separator;

            String osName = System.getProperty("os.name");
            String u = " -u" + mysqlusername;
            String p = " -p" + mysqlpassword;
            String h = StringUtils.isEmpty(ip)?"":" -h" + ip;
            String P = " -P" + port;

            File f = new File(mysqlBackupDir + type);
            if(!f.exists())f.mkdirs();
            List<String> command = new ArrayList<String>();
            if (osName.contains("Windows")) {
                //command.add("cmd");
                //command.add("/c " );
            } else {
                command.add("/bin/sh");
                command.add("-c");

            }

            StringBuffer sb = new StringBuffer();
            if(flag.equals("backup")){
                if (osName.contains("Windows")) {
                    sb.append("cmd /c");
                }else {
                    sb.append(mysqlDir);
                }
                sb.append(" mysqldump");
                sb.append(u + p + h + P);
                sb.append(" --single-transaction --opt ");
                sb.append(dataBaseName).append(" > ");
                sb.append(mysqlBackupDir);
                sb.append(type);
                sb.append("/");
                //sb.append(File.separator);
                sb.append(fileName);
            }
            if(flag.equals("restore")){
//                String str = mysqlDir + "mysql -u" + mysqlusername + " -p" + mysqlpassword + " -h " + ip + " daxt<" + mysqlBackupDir + selectById.getDataType() + File.separator + selectById.getFileName();
                if (osName.contains("Windows")) {
                    sb.append("cmd /c ");
                }else {
                    sb.append(mysqlDir);
                }
                sb.append("mysql");
                sb.append(u + p + h + P + " ");
                sb.append(dataBaseName).append(" < ");
                //sb.append(" daxt<");
                sb.append(mysqlBackupDir);
                sb.append(type);
                sb.append(File.separator);
                sb.append(fileName);
            }

            command.add(sb.toString());
            System.err.println("备份/还原 ："+ sb.toString());
            log.debug("database order：" + sb.toString());

            String[] execStr = command.toArray(new String[command.size()]);

            Process prc =null;
            if (osName.contains("Windows")) {
                System.out.println(sb.toString());
                prc=Runtime.getRuntime().exec(sb.toString());
            } else {
                prc=Runtime.getRuntime().exec(execStr);
            }



            int processComplete = prc.waitFor();


            if (processComplete == 0) {
                return true;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }









    public static void main(String[] args){
//        System.out.println("11:"+getFileName("http://192.168.1.112:7502/file/static/2-2020-0002-003(1).pdf"));
    }



}
